<?php
require_once(SCRIPTS_PATH . '/simple_html_dom/ganon.php');
class Zendvn_CrawlContent{
	
	public function __construct(){
		
	}
	
	public function getPageInfo($url, $domain){
		$arrData = array();
		$response = $this->getContent($url);
		$objHtml =  str_get_dom($response['content']);
		$title = $objHtml('meta[property=og:title]', 0);
		if(!empty($title)){
			$title = $title->getAttribute('content');
		}
		if(empty($title)){
			$title = $objHtml('meta[name=title]', 0);
			if(!empty($title)){
				$title = $title->getAttribute('content');
			}
			if(empty($title)){
				$title = $objHtml('title', 0);
				if(!empty($title)){
					$title = $title->getPlainText();
				}
			}
		}
		
		$desciption = $objHtml('meta[property=og:description]', 0);
		if(!empty($desciption)){
			$desciption = $desciption->getAttribute('content');
		}
		if(empty($desciption)){
			$desciption = $objHtml('meta[name=description]', 0);
			if(!empty($desciption)){
				$desciption = $desciption->getAttribute('content');
			}
		}
		
		$img = $objHtml('meta[property=og:image]', 0);
		if(!empty($img)){
			$img = $img->getAttribute('content');
			self::processImage($img, $domain, $arrImg);
		}
		
		$img = $objHtml('meta[name=msapplication-TileImage]', 0);
		if(!empty($img)){
			$img = $img->getAttribute('content');
			self::processImage($img, $domain, $arrImg);
		}
		
		$imgHtml = $objHtml('img');
		if(!empty($imgHtml)){
			$i = 0;
			foreach ($imgHtml as $elem){
				$img = $elem->getAttribute('src');
				self::processImage($img, $domain, $arrImg);
				if($i == 10){
					break;
				}
				$i++;
			}
		}
		
		$arrImg = array_unique($arrImg);
		$arrData = array(
				'title' 		=> $title,
				'description' 	=> $desciption,
				'domain'		=> $domain,
				'images' 		=> $arrImg
		);
		return $arrData;
	}

	public function processImage($imgUrl, $domain, &$arrImg){
		if(preg_match('/^(http:\/\/[^\/]+)/i', $imgUrl)){
			if(self::checkRemoteFile($imgUrl)){
				$size = getimagesize($imgUrl);
				if($size[0] >= 90 && $size[1] >= 50)
					$arrImg[] = $imgUrl;
			}
		}else{
			if(self::checkRemoteFile($domain . $imgUrl)){
				$size = getimagesize($domain . $imgUrl);
				if($size[0] >= 90 && $size[1] >= 50)
					$arrImg[] = $domain . $imgUrl;
			}
		}
	}
	
	public function getContent($url, $field = array()) {
		$field_string = http_build_query($field);
		$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
		$options = array(
	
				CURLOPT_CUSTOMREQUEST  =>"GET",        //set request type post or get
				CURLOPT_POST           =>false,        //set to GET
				CURLOPT_USERAGENT      => $user_agent, //set user agent
				CURLOPT_RETURNTRANSFER => true,     // return web page
				CURLOPT_HEADER         => false,    // don't return headers
				CURLOPT_FOLLOWLOCATION => true,     // follow redirects
				CURLOPT_ENCODING       => "",       // handle all encodings
				CURLOPT_AUTOREFERER    => true,     // set referer on redirect
				CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
				CURLOPT_TIMEOUT        => 120,      // timeout on response
				CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
		);
		$ch      = curl_init($url);
		curl_setopt_array( $ch, $options );
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		//	curl_setopt($ch, CURLOPT_PROXY, PROXY_IP);
		curl_setopt($ch, CURLOPT_POSTFIELDS,$field_string);
		$content = curl_exec( $ch );
		$err     = curl_errno( $ch );
		$errmsg  = curl_error( $ch );
		$header  = curl_getinfo( $ch );
		curl_close( $ch );
		$header['errno']   = $err;
		$header['errmsg']  = $errmsg;
		$header['content'] = $content;
		return $header;
	}

	public function checkRemoteFile($url){
	    $ch = curl_init();
	    curl_setopt($ch, CURLOPT_URL,$url);
	    // don't download content
	    curl_setopt($ch, CURLOPT_NOBODY, 1);
	    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    if(curl_exec($ch)!==FALSE){
	        return true;
	    }else{
	        return false;
	    }
	}
}